package com.atguigu.serviceedu.service.impl;

import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.serviceedu.contact.Course;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduCourseDescription;
import com.atguigu.serviceedu.mapper.EduCourseMapper;
import com.atguigu.serviceedu.query.CourseQuery;
import com.atguigu.serviceedu.service.*;
import com.atguigu.servicebase.vo.CourseInfoForm;
import com.atguigu.serviceedu.vo.CoursePublishVo;
import com.atguigu.serviceedu.vo.TeacherInfoVo;
import com.atguigu.serviceedu.vo.front.CourseQueryVo;
import com.atguigu.serviceedu.vo.front.CourseWebVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2021-12-19
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService courseDescriptionService;

    @Autowired
    EduSubjectService subjectService;

    @Autowired
    EduChapterService chapterService;

    @Autowired
    EduVideoService videoService;


    @Override
    @Transactional
    public void addCourseInfo(CourseInfoForm courseInfoForm) {
        //课程表里添加数据
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus(Course.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        eduCourse.setSubjectParentId(courseInfoForm.getSubjectParentId());
        boolean resultCourseInfo = this.save(eduCourse);
        if(!resultCourseInfo){
            throw new GuliException(20001, "课程信息保存失败");
        }
        //课程简介表里添加数据
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(eduCourse.getId());
        boolean resultDescription = courseDescriptionService.save(courseDescription);
        if(!resultDescription){
            throw new GuliException(20001, "课程详情信息保存失败");
        }
    }

    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoForm courseInfoVo = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2 查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoVo) {
        //1 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2 修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void CoursePageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");

        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();


        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(status) ) {
            queryWrapper.eq("status", status);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void removeCourseById(String id) {
        //根据课程id删除课程对应的所有小节
        videoService.removeVideoByCourseId(id);
        //删除课程对应的所有章节
        chapterService.removeByCourseId(id);
        //删除课程
        this.removeById(id);
    }

    @Override
    public void updateCourseStatusById(String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus(Course.COURSE_NORMAL);
        this.updateById(eduCourse);
    }
    @Cacheable(value = "course",key = "'getHotCoures'")
    @Override
    public List<EduCourse> getHotCoures() {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>().orderByDesc("view_count");
        queryWrapper.last("limit 8");
        List<EduCourse> eduCourses = baseMapper.selectList(queryWrapper);
        return eduCourses;
    }

    @Override
    public List<EduCourse> getTeacherCourseList(String teacherId) {
        List<EduCourse> eduCourseList = baseMapper.selectList(new QueryWrapper<EduCourse>().eq("teacher_id", teacherId));
        return eduCourseList;
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, queryWrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo selectInfoWebById(String courseId) {
        this.updatePageViewCount(courseId);
        CourseWebVo courseWebVo = baseMapper.selectInfoWebById(courseId);
        return courseWebVo;
    }

    @Override
    public void updatePageViewCount(String id) {
        EduCourse course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);
    }

    @Override
    public String getTeacherNameByCourseId(String courseId) {
        TeacherInfoVo teacher = baseMapper.getTeacherNameByCourseId(courseId);
        return teacher.getTeacherName();
    }

}
